#!/usr/bin/env node

var io = require('socket.io-client');

var url = 'http://localhost:8082';

var mobile = io.connect(url+'/mobile');

var socket = null;

var lat = -31.4086;
var lng = -64.19169;

mobile.on('connect', function() {
  console.log('mobile connected');
  mobile.on('createMobile', function(id) {
    socket = io.connect(url+'/'+id+'/mobile');
    var itv = 500;
    socket.on('connect', function() {
      console.log('socket connected with id: '+id);
      var r = function() {
	var i = Math.floor(Math.random()*10)/10000;
	if(Math.round(Math.random())) {
	  i = i*-1;
	}
	lat = lat + i;
	i = Math.floor(Math.random()*10)/10000;
	if(Math.round(Math.random())) {
	  i = i*-1;
	}
	lng = lng + i;
	console.log('emmiting update to: '+lat+','+lng);
	socket.emit('mobilePosition', {lat: lat, lng: lng});
	setTimeout(r, itv);
      };
      r();
    });
    socket.on('slower', function() {
      itv = itv*10;
    });
  });
  console.log('requesting mobile');
  mobile.emit('requestMobile');
});

